package com.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.dao.ContactDAO;
import com.dao.ProgrammeDAO;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.pojo.Contact;
import com.pojo.Programme;

import com.vo.ProgrammeVO;

public class ProgrammeAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = -908994404000031994L;
	List<ProgrammeVO> list = null;
	ProgrammeVO pvo = null;
	private String inActive = "false";
	private String listInactiveDir;
	private String errname;

	public String getErrname() {
		return errname;
	}

	public void setErrname(String errname) {
		this.errname = errname;
	}

	public String getListInactiveDir() {
		return listInactiveDir;
	}

	public void setListInactiveDir(String listInactiveDir) {
		this.listInactiveDir = listInactiveDir;
	}

	public String isInActive() {
		return inActive;
	}

	public void setInActive(String inActive) {
		this.inActive = inActive;
	}

	public ProgrammeAction() {
		list = new ArrayList<ProgrammeVO>();
		pvo = new ProgrammeVO();
	}

	public ProgrammeVO getPvo() {
		return pvo;
	}

	public void setPvo(ProgrammeVO pvo) {
		this.pvo = pvo;
	}

	public List<ProgrammeVO> getList() {
		return list;
	}

	public void setList(List<ProgrammeVO> list) {
		this.list = list;
	}

	public String list() throws Exception {
		String isActive;
		if (inActive.equals("false")) {
			isActive = "Yes";
		} else
			isActive = "No";

		list = new ProgrammeDAO().getAll(isActive);
		return "success";
	}

	public String filterIsActive() throws Exception {
		HttpServletRequest request = (HttpServletRequest) ActionContext
				.getContext().get(ServletActionContext.HTTP_REQUEST);
		String filterName = request.getParameter("filterName");
		request.getSession().setAttribute("filter", filterName);
		String isActive;
		if (inActive.equals("false")) {
			isActive = "Yes";
		} else
			isActive = "No";

		list = new ProgrammeDAO().getByFilterName(filterName, isActive);
		return "success";
	}

	public String edit() {
		HttpServletRequest request = (HttpServletRequest) ActionContext
				.getContext().get(ServletActionContext.HTTP_REQUEST);
		int programmeId = Integer.parseInt(request.getParameter("programmeId"));
		pvo = new ProgrammeDAO().getProgrammeById(programmeId);
		if (pvo.getIsActive().equals("Yes")) {
			pvo.setIsActive("true");
		} else
			pvo.setIsActive("false");
		return "success";
	}

	public String addAction() {
		return SUCCESS;
	}

	public String insertAction() {

		if (!(new ProgrammeDAO().checkProgrammeName(pvo.getProgrammeName()))) {
			setErrname("Name is exits");
			return ERROR;
		} else {

			Contact c = new ContactDAO().getContactByContactNumber(pvo
					.getContactNumber());
			Programme p = new Programme();

			if (pvo.getIsActive().equals("true")) {

				pvo.setIsActive("Yes");
			} else
				pvo.setIsActive("No");
			p.setProgrammeId(pvo.getProgrammeId());
			p.setProgrammeName(pvo.getProgrammeName());
			p.setDescription(pvo.getDescription());
			p.setIsActive(pvo.getIsActive());
			p.setContact(c);
			new ProgrammeDAO().insertProgramme(p);
			return SUCCESS;
		}

	}

	public String updateAction() {
		HttpServletRequest request = (HttpServletRequest) ActionContext
				.getContext().get(ServletActionContext.HTTP_REQUEST);
		int programmeId = Integer.parseInt(request.getParameter("programmeId"));
		
			if (pvo.getIsActive().equals("true")) {

				pvo.setIsActive("Yes");
			} else
				pvo.setIsActive("No");

			new ProgrammeDAO().updateProgramme(pvo, programmeId);
			return SUCCESS;

		

	}

	public String deleteAction() {
		HttpServletRequest request = (HttpServletRequest) ActionContext
				.getContext().get(ServletActionContext.HTTP_REQUEST);
		int programmeId = Integer.parseInt(request.getParameter("programmeId"));
		new ProgrammeDAO().deleteProgramme(programmeId);
		return SUCCESS;
	}

}
